<?php

namespace app\admin\controller\work_area;

use app\admin\service\work_area\BusinessService;
use app\common\controller\Backstage;
use app\common\service\RedisQuantity;
use hg\apidoc\annotation as Apidoc;
use thans\jwt\facade\JWTAuth;
use think\facade\Env;

/**
 * @Apidoc\Title("工区管理")
 */
class WorkArea extends Backstage
{
    //中间件不验证的方法
    protected $middleware = [
        'app\\middleware\\Admin' => [
            'except' => ['info', 'addrSearch']
        ]
    ];

    /**
     * @Apidoc\Title("工区列表")
     * @Apidoc\Url("/admin/work_area.WorkArea/listApi")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("keyword", type="string", desc="关键字")
     * @Apidoc\Param("keyword_time", type="array", desc="数组0开始 1结束")
     * @Apidoc\Param("page", type="int", desc="第几页默认第1页")
     * @Apidoc\Param("page_size", type="int", desc="每页几条默认10条")
     * @Apidoc\Returned("data", type="array", desc="返回说明")
     * @Apidoc\Returned("total", type="int", desc="总条数")
     */
    public function listApi()
    {
        try {
            $param = $this->request->param();
            $res = BusinessService::WorkAreaList($param);
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return $this->ajax(0, $e->getMessage());
        }
        return $this->ajax(1, '成功', $res);
    }


    /**
     * @Apidoc\Title("工区详情")
     * @Apidoc\Url("/admin/work_area.WorkArea/info")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("id",type="int",require=true, desc="劳务单位id")
     * @Apidoc\Returned("data", type="object", desc="对象信息",
     *    @Apidoc\Returned("name", type="string", desc="工区名称"),
     *    @Apidoc\Returned("addr", type="string", desc="地址"),
     *    @Apidoc\Returned("longitude", type="string", desc="经度"),
     *    @Apidoc\Returned("latitude", type="string", desc="纬度"),
     *    @Apidoc\Returned("sort", type="int", desc="排序"),
     *    @Apidoc\Returned("remarks", type="string", desc="备注"),
     * )
     */
    public function info()
    {
        try {
            $param = $this->request->param();
            Validate([
                'id|系统错误' => 'require',
            ])->check($param);
            $res = BusinessService::workAreaInfo($param);
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return $this->ajax(0, $e->getMessage());
        }
        return $this->ajax(1, '获取成功', $res);
    }


    /**
     * @Apidoc\Title("地图地址搜索")
     * @Apidoc\Url("/admin/work_area.WorkArea/addrSearch")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("keyword",type="string",require=true, desc="搜索地址")
     * @Apidoc\Returned("data", type="object", desc="返回说明")
     */
    public function addrSearch()
    {
        try {
            $param = $this->request->param();
            JWTAuth::auth();
            Validate([
                'keyword|搜索空' => 'require',
            ])->check($param);
            $data = [
                'key' => Env::get('TencentCloud.map'),
                'keyword' => $param['keyword'],
                'boundary' => 'nearby(30.655831,104.067649,1000,1)',
            ];
            $url = "https://apis.map.qq.com/ws/place/v1/search?" . http_build_query($data);
            $res = httpGet($url);
            $res = json_decode($res, true);
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return $this->ajax(0, $e->getMessage());
        }
        return $this->ajax(1, '获取成功', $res);
    }

    /**
     * @Apidoc\Title("添加,修改")
     * @Apidoc\Url("/admin/work_area.WorkArea/edit")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("id",type="int", desc="修改传id")
     * @Apidoc\Param("name",type="string",require=true, desc="名称")
     * @Apidoc\Param("addr", type="string",require=true, desc="地址")
     * @Apidoc\Param("longitude", type="string",require=true, desc="经度")
     * @Apidoc\Param("latitude", type="string",require=true, desc="纬度")
     * @Apidoc\Param("sort", type="int", desc="排序")
     * @Apidoc\Param("remarks", type="string", desc="备注")
     * @Apidoc\Returned("data", type="object", desc="返回说明")
     */
    public function edit()
    {
        try {
            $param = $this->request->param();
            $result = JWTAuth::auth();
            $admin_id = $result['admin_id'];
            $data = [
                'name|名称' => 'require',
                'addr|地址' => 'require'
            ];
            Validate($data)->check($param);
            BusinessService::WorkAreaSave($param, $admin_id);
            if (!RedisQuantity::FrequencyLimit(md5(request()->controller() . request()->action() . $admin_id), 2)) {
                throw new \Exception('请求频繁,稍后再试');
            }
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return $this->ajax(0, $e->getMessage());
        }
        return $this->ajax(1, '操作成功');
    }


    /**
     * @Apidoc\Title("删除")
     * @Apidoc\Url("/admin/work_area.WorkArea/del")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("id",type="int",require=true, desc="id")
     * @Apidoc\Returned("data", type="object", desc="对象信息")
     */
    public function del()
    {
        try {
            $param = $this->request->param();
            Validate([
                'id|系统错误' => 'require',
            ])->check($param);
            \app\common\model\work_area\WorkArea::destroy($param['id']);
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return $this->ajax(0, $e->getMessage());
        }
        return $this->ajax(1, '操作成功');
    }

}